﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SignalShop;
using SignalShopBase;

namespace SignalShopUI
{
    public partial class MainForm : Form
    {
        private ModuleRepository moduleRepository;
        private SignalRepository signalRepository;

        private Signal signal1;
        private Signal signal2;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            moduleRepository = new ModuleRepository();
            signalRepository = new SignalRepository();

            moduleRepository.ModuleLoaded += new ModuleRepositoryModuleLoadedEventHandler(moduleRepository_ModuleLoaded);

            signal1 = new Signal(512);
            signal2 = new Signal(512);

            panel1.Chart.Signal = signal1;
            panel2.Chart.Signal = signal2;

            panel1.Chart.Selected = true;

            panel1.Chart.ChartSelected += new ChartEventHandler(Chart1_ChartSelected);
            panel2.Chart.ChartSelected += new ChartEventHandler(Chart2_ChartSelected);
            
        }

        void Chart1_ChartSelected(Chart source, ChartEvent e)
        {
            panel2.Chart.Selected = false;            
            panel2.Chart.Refresh();
        }

        void Chart2_ChartSelected(Chart source, ChartEvent e)
        {
            panel1.Chart.Selected = false;
            panel1.Chart.Refresh();
        }

        void moduleRepository_ModuleLoaded(ModuleRepository source, ModuleRepositoryModuleLoadedEventArgs e)
        {
            AddMenuItem(this.menuStrip1.Items, e.Module, e.Module.Path);
        }

        private void AddMenuItem(ToolStripItemCollection parent, SignalShopModule module, string path)
        {
            if (path.Contains('/'))
            {
                string childName = path.Substring(0, path.IndexOf('/'));

                var child = (ToolStripMenuItem)parent[childName];

                if (child == null)
                {
                    child = new ToolStripMenuItem(childName);
                    child.Name = childName;
                    parent.Add(child);
                }

                AddMenuItem(child.DropDown.Items, module, path.Substring(path.IndexOf('/') + 1));
            }
            else
            {
                var menuItem = new ToolStripMenuItem(path);
                menuItem.Name = module.Path;
                menuItem.Text = module.Name;
                parent.Add(menuItem);

                menuItem.Click += new EventHandler(menuItem_Click);
            }
        }

        void menuItem_Click(object sender, EventArgs e)
        {
            string path = ((ToolStripMenuItem)sender).Name;

            var module = moduleRepository.Modules.Get(path);

            module.Execute(panel1.Chart.Selected ? signal1 : signal2, panel2.Chart.Selected ? signal1 : signal2);

            panel1.Chart.Refresh();
            panel2.Chart.Refresh();
        }

        private void importMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // TODO copy .dll files to modules folder and import from there

                moduleRepository.Load(ofd.FileNames);
            }
        }
    }
}
